package database;


import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import java.sql.ResultSet;
import java.sql.SQLException;

import org.junit.Before;
import org.junit.Test;

public class testMySQLServer {
	MySQLServer testServer = new MySQLServer("localhost","root","usbw");
	final String TABLENAME = "clientlist";
	final String HENK1 = " givenname = 'Henk' ,"  + " lastname = 'Vries', " + " address = 'Delft', " + " phone = '0900henk', " + " email = 'Delft@mail.com' ";
	final String HENK2 = " givenname = 'Henk2' ,"  + " lastname = 'Vries2', " + " address = 'Delft2', " + " phone = '0900henk', " + " email = 'Delft@mail.com' ";
	
	@Before
	public void setup() throws SQLException {
		testServer.update("TRUNCATE TABLE " + "clientList" + ";"); // tabel leegmaken
	}
	
	@Test
	public void test_testConnection() {
		assertTrue(testServer.testConnection());
		
		MySQLServer wrongServer = new MySQLServer("localhost","root","IDONTKNOW");
		assertFalse(wrongServer.testConnection());
	}
	
	@Test
	public void testConstructor() {
		MySQLServer server = new MySQLServer("host", "user", "password");
		assertEquals("host", server.getHost());
		assertEquals("user", server.getUser());
		assertEquals("password", server.getPassword());
	}
	
	@Test
	public void testUpdateSelect() throws SQLException {
		testServer.update("INSERT INTO " + TABLENAME + " SET id = 1, " + HENK1 +";");
		testServer.update("INSERT INTO " + TABLENAME + " SET id = 2, " + HENK2 +";");
		
		ResultSet rs = testServer.select("SELECT * FROM " + "clientlist" + ";");
		rs.next();
		assertEquals( 1 ,rs.getInt("id"));
		assertEquals( "Henk" ,rs.getString("givenname"));
		rs.next();
		assertEquals( 2 ,rs.getInt("id"));
		assertEquals( "Henk2" ,rs.getString("givenname"));
	}
	
	@Test
	public void testDisConnectDB() throws SQLException {
		testServer.select("SELECT * FROM clientlist;");
		assertEquals(true, testServer.disconnect());
	}
}
	